package com.csjbot.blackgaga.feature.settings.charge_setting;

import android.content.Context;

import com.csjbot.blackgaga.R;
import com.csjbot.blackgaga.util.SharedKey;
import com.csjbot.blackgaga.util.SharedPreUtil;

/**
 * author : chenqi.
 * e_mail : 1650699704@163.com.
 * create_time : 2017/10/20.
 */

public class ChargeSettingPresenter implements ChargeSettingContract.presenter {
    private Context context;
    private ChargeSettingContract.view view;
    private String[] args;
    public static boolean CHECKSTATE;
    private String CHARGEINF;
    private String SPLITE = "%";
    private int CODE;

    public ChargeSettingPresenter(Context context) {
        this.context = context;
        args = context.getResources().getStringArray(R.array.electricitys);
    }


    @Override
    public ChargeSettingContract.view getView() {
        return view;
    }

    @Override
    public void initView(ChargeSettingContract.view view) {
        this.view = view;
    }

    @Override
    public void releaseView() {
        if (view != null) {
            view = null;
        }
    }

    @Override
    public boolean getChargestate() {
        CHECKSTATE = SharedPreUtil.getBoolean(SharedKey.CHARGING_NAME, SharedKey.IS_CHARGING);
        return CHECKSTATE;
    }

    @Override
    public String getChargeInfor() {
        if (SharedPreUtil.getString(SharedKey.CHARGING_NAME, SharedKey.CHARGING_INFOR) != null) {
            CODE = Integer.parseInt(SharedPreUtil.getString(SharedKey.CHARGING_NAME, SharedKey.CHARGING_INFOR).split(SPLITE)[0]);
            return SharedPreUtil.getString(SharedKey.CHARGING_NAME, SharedKey.CHARGING_INFOR);
        } else {
            CODE = ChargeManager.charging;
            return CODE + "%";
        }
    }

    @Override
    public int addCount(int code) {
        int current = CODE + code;
        if (current > 50) {
            view.showCodeMax();
            return CODE;
        } else if (current < 20) {
            view.showCodeMin();
            return CODE;
        } else {
            CODE = current;
            return current;
        }
    }


    @Override
    public void saveCheckState(boolean b) {
        this.CHECKSTATE = b;
    }

    @Override
    public void save(int i) {
        ChargeManager.charging = CODE;
        SharedPreUtil.putString(SharedKey.CHARGING_NAME, SharedKey.CHARGING_INFOR, CHARGEINF);
        SharedPreUtil.putBoolean(SharedKey.CHARGING_NAME, SharedKey.IS_CHARGING, CHECKSTATE);
        view.saveShowDialog(i);
    }


    @Override
    public void saveChargeInf(String arg) {
        this.CHARGEINF = arg;
    }

}
